﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;

using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NCommon;


namespace DingApi.Controllers
{
    [RoutePrefix("api/Paso04p2")]
    public class Paso04p2Controller : ApiController
    {   

        [HttpPost]
        [Route("PostPaso04p2")]

        //多记录查询
        public System.Data.DataSet PostPaso04p2([FromBody]  JObject obj)
        {

            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
            EI.EIInfo inBlock = new EI.EIInfo();
          

            inBlock.Tables[0].Columns.Add("ASMAT_NAME");
            inBlock.Tables[0].Columns.Add("APPLY_BILL_NO");      
            inBlock.Tables[0].Columns.Add("BRANCH_CODE");        
            inBlock.Tables[0].Columns.Add("DPTMENT_CODE"); 
     
            inBlock.Tables[0].Columns.Add("AUDIT_FLAG");  
       
            inBlock.Tables[0].Columns.Add("REC_CREATE_TIME");    
            inBlock.Tables[0].Columns.Add("REC_REVISE_TIME");    

          



            inBlock.Tables[0].Rows.Add();

            inBlock.sys_info.company_code   = "LT";
            inBlock.sys_info.userid         = obj["USERID"].ToString();
            inBlock.sys_info.formname       = obj["FORMNAME"].ToString();   

            inBlock.Tables[0].Rows[0]["ASMAT_NAME"]        =  obj["ASMAT_NAME"].ToString();
            inBlock.Tables[0].Rows[0]["APPLY_BILL_NO"]     =  obj["APPLY_BILL_NO"].ToString();
            inBlock.Tables[0].Rows[0]["BRANCH_CODE"]       =  obj["BRANCH_CODE"].ToString();
            inBlock.Tables[0].Rows[0]["DPTMENT_CODE"]      =  obj["DPTMENT_CODE"].ToString();

            //AUDIT_FLAG借用
            inBlock.Tables[0].Rows[0]["AUDIT_FLAG"]         =  obj["AUDIT_FLAG"].ToString();      
   
            inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"]    =  obj["REC_CREATE_TIME"].ToString();
            inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"]    =  obj["REC_REVISE_TIME"].ToString();


            //仅查询最近最近2天的；不然数据太多，影响小程序效率；
            if (inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"].ToString().Trim()=="")
            {
                inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"] =    DateTime.Now.AddDays(-2).ToString("yyyyMMddHHmmss");
            }

            


            EI.EIInfo outBlock = EI.EITuxedo.CallService("paso04p2_inq", inBlock);

            //小代码转换；
            EI.EIInfo outBlock_TEMP = EF.Utility.GetCodeClassValue("PAA1","PAAJ", "FIBR", "FIDP");
            DataTable dtTemp = null;

        

            for (int i = outBlock.Tables[0].Rows.Count - 1; i >= 0; i--)
            {
                 

                //审核标识
                string APN = outBlock.Tables[0].Rows[i]["APN"].ToString().Trim();
                if (APN.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PAA1"].Select("CODE  = '" + APN + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[i]["APN"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


                string CHECK_FLAG1 = outBlock.Tables[0].Rows[i]["CHECK_FLAG1"].ToString().Trim();
                if (CHECK_FLAG1.Trim() != "")
                {
                    if (outBlock_TEMP.Tables["PAAJ"].Select("CODE  = '" + CHECK_FLAG1 + "'").Count<DataRow>() == 1)
                    {
                        dtTemp = outBlock_TEMP.Tables["PAAJ"].Select("CODE  = '" + CHECK_FLAG1 + "'").CopyToDataTable<DataRow>();
                        outBlock.Tables[0].Rows[i]["CHECK_FLAG1"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                    }
                }
                 
                   

                 //BRANCH_CODE 反馈分公司 
                string BRANCH_CODE = outBlock.Tables[0].Rows[i]["BRANCH_CODE"].ToString().Trim();
                if (BRANCH_CODE.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIBR"].Select("CODE  = '" + BRANCH_CODE + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[i]["BRANCH_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


                // 
                string DPTMENT_CODE = outBlock.Tables[0].Rows[i]["DPTMENT_CODE"].ToString().Trim();
                if (DPTMENT_CODE.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIDP"].Select("CODE  = '" + DPTMENT_CODE + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[i]["DPTMENT_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];



                string APPLY_DATE = outBlock.Tables[0].Rows[i]["APPLY_DATE"].ToString();
                if (APPLY_DATE.Length >= 8)
                    outBlock.Tables[0].Rows[i]["APPLY_DATE"] = APPLY_DATE.Substring(0, 4) + "-" + APPLY_DATE.Substring(4, 2) + "-" + APPLY_DATE.Substring(6, 2);

                string AUDIT_TIME = outBlock.Tables[0].Rows[i]["CHECK_DATE"].ToString();
                if (AUDIT_TIME.Length >= 8)
                outBlock.Tables[0].Rows[i]["CHECK_DATE"] = AUDIT_TIME.Substring(0, 4) + "-" + AUDIT_TIME.Substring(4, 2) + "-" + AUDIT_TIME.Substring(6, 2);


            }

            
            
            outBlock.Tables[0].TableName = "Table0";


            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);            
            return outBlock;


        }


        //[HttpPost]
        //[Route("PostPaso04p2_Info")]


        [HttpPost]
        [Route("PostPaso04p2_load")]

        //高级查询的查询条件获取，【主要是各类小代码，类似之前画面的初始化参数】

        public System.Data.DataSet PostPaso04p2_load([FromBody]  JObject obj)
        {

           
            //小代码转换； , "FIDP"
            EI.EIInfo outBlock = EF.Utility.GetCodeClassValue("FIBR");

           

            DataRow dr4 = outBlock.Tables["FIBR"].NewRow();
            dr4["CODE"] = " ";
            dr4["CODE_DESC_1_CONTENT"] = " ";
            outBlock.Tables["FIBR"].Rows.InsertAt(dr4, 0);

            //DataRow dr5 = outBlock.Tables["FIDP"].NewRow();
            //dr5["CODE"] = " ";
            //dr5["CODE_DESC_1_CONTENT"] = " ";
            //outBlock.Tables["FIDP"].Rows.InsertAt(dr5, 0);

            

            string str = "select distinct BRANCH_CODE, a.DPTMENT_CODE as code, b.CODE_DESC_1_CONTENT ";
            str += " from tsifiaa71 a, tep0002 b  ";
            str += " where  b.code_class = 'FIDP' ";
            str += " and a.DPTMENT_CODE = b.code  ";
            str += " and b.code_desc_5_content in ('2', '3') ";
            str += " order by a.dptment_code ";

            EI.EIInfo outBlock2 = EF.Utility.ExecQuery(str);

            outBlock2.Tables[0].TableName = "FIDP";


            outBlock.Tables.Add(outBlock2.Tables[0].Copy());
             

            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }


        [HttpPost]
        [Route("PostPaso04p2_pro")]

        //审核、驳回、审核撤销
        public System.Data.DataSet PostPaso04p2_pro([FromBody]  JObject obj)
        {



            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
            EI.EIInfo inBlock = new EI.EIInfo();


                    //          ACTION: TPASO04_P50
                    //APPLY_BILL_NO: AL22A03876
                    //ASMAT_NO: BJI0300127
                    //CHECK_RESULT:  
                    //COMPANY_CODE: LT
                    //FORMNAME: PASO04P2P
                    //PURCHASE_APPLY_BILL_NO:  
                    //USERID: admin
                    //USERNAME: 王标

          
          
           
            inBlock.Tables[0].Columns.Add("APPLY_BILL_NO");    
            inBlock.Tables[0].Columns.Add("COMPANY_CODE");     
            inBlock.Tables[0].Columns.Add("ASMAT_NO");     
            inBlock.Tables[0].Columns.Add("PURCHASE_APPLY_BILL_NO");     
 
           

            inBlock.Tables[0].Columns.Add("CHECK_RESULT"); //必须有此值


            



          

            inBlock.Tables[0].Rows.Add();
            inBlock.sys_info.company_code   =   "LT";
            inBlock.sys_info.userid = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            inBlock.sys_info.username = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();
            inBlock.sys_info.formname = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();


            inBlock.Tables[0].Rows[0]["APPLY_BILL_NO"] = obj["APPLY_BILL_NO"] == null ? " " : obj["APPLY_BILL_NO"].ToString();
            inBlock.Tables[0].Rows[0]["COMPANY_CODE"] = obj["COMPANY_CODE"] == null ? " " : obj["COMPANY_CODE"].ToString();
            inBlock.Tables[0].Rows[0]["ASMAT_NO"] = obj["ASMAT_NO"] == null ? " " : obj["ASMAT_NO"].ToString();
            inBlock.Tables[0].Rows[0]["PURCHASE_APPLY_BILL_NO"] = obj["PURCHASE_APPLY_BILL_NO"] == null ? " " : obj["PURCHASE_APPLY_BILL_NO"].ToString();


            inBlock.Tables[0].Rows[0]["CHECK_RESULT"] = obj["CHECK_RESULT"] == null ? " " : obj["CHECK_RESULT"].ToString();
          	 

            
            inBlock.Tables[0].TableName = obj["ACTION"].ToString();
            EI.EIInfo outBlock = EI.EITuxedo.CallService("paso04p2_save", inBlock);


            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }

    }
}